
public class Matrices {
	public static int m(int i, int j){
		if(i==j || i==-1 || j==-1) return 0;
		int min = Integer.MAX_VALUE;
		for(int k=i; k!=j; k++){
			int mi = m(i,k) + m(k+1,j) + matrices[i-1][0] * matrices[k][1] * matrices[j][1];
			if(mi < min && mi!=0) min = mi;
		}
		return min;
	}

	public static int[][] matrices = {{0,0},{2,3},{3,2}};
	static int[][] arr = new int[matrices.length+1][matrices.length+1];
	public static void main(String[] args) {
		System.out.println(m(1,matrices.length-1));
		System.out.println("Dynamiccc "+dinamica());
		//show();
	}
	
	
	public static int dinamica(){
	
	int i=1, j=1;
	int jota = 1;
		while(i!=1 || j!=matrices.length-1){
			if(j>=matrices.length-1){
				i=1; jota++; j = jota;
			}
			else{ i++; j++;}
			//System.out.println("i "+i+" j"+j);
			
			int s = Integer.MAX_VALUE;
			for(int k=i; k<j; k++){
				int v = arr[i][k] + arr[k++][j] + (matrices[i][0] * matrices[j][1] * matrices[k][1]);
				if(v<s && v>0) s = v;
			}
			arr[i][j] = s;
		}
		return arr[1][matrices.length-1];
	}
	
	
	
	
	public static void show() {
        for (int i = 0; i < arr[0].length; i++) {
            for (int j = 0; j < arr[1].length; j++) 
                System.out.print(arr[i][j]+" ");
            System.out.println();
        }
    }

}
